package com.coderdream.chapter15.facade.a2;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PageMaker {
	private PageMaker() {
	}

	public static void makeWelcomePage(String mailaddr, String filename) {
		try {
			Properties mailprop = Database.getProperties("maildata");
			String username = mailprop.getProperty(mailaddr);
			HtmlWriter hw = new HtmlWriter(new FileWriter(filename));
			hw.title("Welcome to " + username + "'s page!");
			hw.paragraph("歡迎來到" + username + "的網頁");
			hw.mailto(mailaddr, username);
			hw.close();
			System.out.println(filename + " is created for " + mailaddr + " ("
					+ username + ")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param filename
	 */
	public static void makeLinkPage(String filename) {
		try {
			HtmlWriter hw = new HtmlWriter(new FileWriter(filename));
			hw.title("Link page");
			Properties mailprop = Database.getProperties("maildata");
			Enumeration en = mailprop.propertyNames();
			while (en.hasMoreElements()) {
				String mailaddr = (String) en.nextElement();
				String username = mailprop.getProperty(mailaddr, "(unknown)");
				hw.mailto(mailaddr, username);
			}

			hw.close();
			System.out.println(filename + " is created.");

		} catch (IOException e) {
			// TODO: handle exception
		}
	}
}
